package com.iwhalecloud.citybrain.flow.platform.manager.application.service;

import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.CandidateRelationShipEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 　　* @description: 流程节点候选适配服务
 　　* @author shenrm
 　　* @date 2021/11/29 17:01
 　　*/
@Component
public class FlowCandidateServiceAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRelationShipService userRelationShipService;

    /**
     * 控制开关true是打开,false是关闭
     */
    @Value("${apply.user.enable:true}")
    private boolean controlSwith;

    /**
     * 获取拟稿人
     *
     * @param processInstanceId
     * @return
     */
    public String getNgrUser(String processInstanceId, String userCode) {
        FlowCandidateService flowCandidateService = null;
        if(controlSwith) {
            flowCandidateService = SpringUtils.getBean("flowTaskCandidateService",FlowCandidateService.class);
        } else {
            flowCandidateService = SpringUtils.getBean("flowApplyUserService", FlowCandidateService.class);
        }
        if(Objects.isNull(flowCandidateService)) {
            return null;
        }
        return flowCandidateService.getNgrUser(processInstanceId, userCode);
    }


    /**
     * 处理后续节点候选人是【关系】这种类型
     *
     * @param dataType
     * @param code
     * @param processInstanceId
     * @param userCode
     * @return
     * @throws Exception
     */
    public Set<UserInfo> handleNextNodeCandidateUsers(String dataType, String code, String processInstanceId, String userCode) throws Exception {
        // 否则,开始组装用户信息集合
        Set<UserInfo> userInfoSet = new HashSet<>();

        if (Objects.equals(dataType,"RELATIONSHIP")){
            // 获取当前流程节点的处理人,默认取该整个流程实例的第一个节点处理人,配置关闭,则取流程发起人
            String ngrUserId = this.getNgrUser(processInstanceId, userCode);

            // 如果设置的是拟稿人或拟稿人部门并且拟稿人为空,则直接抛出异常
            if ((Objects.equals(code, CandidateRelationShipEnum.NGR.getCode()) ||
                    Objects.equals(code, CandidateRelationShipEnum.NGRBM.getCode())) &&
                    StringUtils.isBlank(ngrUserId)) {
                throw new Exception(ErrorCode.NGR_USER_NOT_EXIST.errMessage());
            }

            // 如果是拟稿人,则直接添加ngrUserId
            if (Objects.equals(code, CandidateRelationShipEnum.NGR.getCode())) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserCode(ngrUserId);
                UserInfo userInfoObj = userService.getUserInfoObj(ngrUserId);
                userInfo.setUserName(userInfoObj == null ? "" : userInfoObj.getUserName());
                userInfoSet.add(userInfo);
            } else if (Objects.equals(code, CandidateRelationShipEnum.NGRBM.getCode())) { // 如果是拟稿人部门,则根据拟稿人获取对应的部门列表,再根据所属的部门列表获取对应的人员列表
                Set<UserInfo> ngrBmUserInfoSet = userRelationShipService.getUserInfoSetByUserId(ngrUserId);
                userInfoSet.addAll(ngrBmUserInfoSet);
            } else if (Objects.equals(code, CandidateRelationShipEnum.DQRBM.getCode())) {
                // 再根据该节点处理人获取对应的部门以及部门对应下的人员信息列表
                Set<UserInfo> dqrBmUserInfoSet = userRelationShipService.getUserInfoSetByUserId(userCode);
                userInfoSet.addAll(dqrBmUserInfoSet);
            }
            return userInfoSet;
        }
        return null;
    }
}
